# ———————————————————————–
# <copyright file="CreateQueue.ps1" company="The MITRE Corporation">
#    Copyright (c) 2010 The MITRE Corporation. All rights reserved.
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#    http:#www.apache.org/licenses/LICENSE-2.0
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
# </copyright>
# ———————————————————————–
################################################/
# CreateQueue.ps1 - Helper Function that creates an MSMQ Queue
# Project: IC.NET Installer
# Author: Don McGarry
################################################/

# Copyright (c) 2013 The MITRE Corporation. All rights reserved.
#
# NOTICE
# This software was produced for the U. S. Government
# under Contract No. FA8721-09-C-0001, and is
# subject to the Rights in Noncommercial Computer Software
# and Noncommercial Computer Software Documentation Clause
# (DFARS) 252.227-7014 (JUN 1995)

function Usage
{
    Write-Host "Usage :"
    Write-Host ""
    Write-Host "    CreateQueue.ps1 <-c,d> <queuename> <Y/N - Private> <user1>  <user2> <all:restrictedPermission> [T:Transactional]"
    Write-Host ""
    exit;
}

$opeartion = $Args[0]
$queuename = $Args[1]
$private = $Args[2]
$User1 = $Args[4]
$User2 = $Args[5]
$permission = $Args[6]
$pTransactional = $Args[3]


if($opeartion -eq $null)
{
    Usage;
}

if(($opeartion -ieq "-c") -and (($queuename -eq ".\") -or ($User1 -eq $null) -or ($private -eq $null) -or ($permission -eq $null)))
{  
  Usage;  
}
elseif(($opeartion -ieq "-d") -and (($queuename -eq ".\") -or ($private -eq $null) ))
{  
  write-host "Cannot delete queue without queuename."
  Usage;
}


[Reflection.Assembly]::LoadWithPartialName("System.Messaging")


if ($private -ieq "Y")
{ 
  $queuename = ".\private$\" + $Args[1]
}

if ($opeartion -ieq "-c")
{
    if ($pTransactional -ieq "T")
    {
        $transactional = 1
        Write-Host "Creating a transactional queue."
    }
    else
    {
        $transactional = 0
        Write-Host "Creating a non-trasactional queue"
    }
    
	if([System.Messaging.MessageQueue]::Exists($queuename))
	{
		Write-Host "Queue Already Exists..."
		return
	}
	
    $qb = [System.Messaging.MessageQueue]::Create($queuename, $transactional) 
    
    if($qb -eq $null)
    {
        exit
    }
    
    $qb.label = $queuename
           
    if ($permission -ieq "all")
    {
        Write-Host "Granting all permissions to "  $User1
        $qb.SetPermissions($User1, [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Allow) 
		
		Write-Host "Granting all permissions to "  $User2
        $qb.SetPermissions($User2, [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Allow) 
    }
    else
    {
        Write-Host "Restricted Control for user: "  $User1
        Write-Host ""
        $qb.SetPermissions($User1, [System.Messaging.MessageQueueAccessRights]::PeekMessage, [System.Messaging.AccessControlEntryType]::Allow) 
        $qb.SetPermissions($User1, [System.Messaging.MessageQueueAccessRights]::ReceiveJournalMessage, [System.Messaging.AccessControlEntryType]::Allow)
	$qb.SetPermissions($User1, [System.Messaging.MessageQueueAccessRights]::WriteMessage, [System.Messaging.AccessControlEntryType]::Allow)
    }

    $qb.SetPermissions("BUILTIN\Administrators", [System.Messaging.MessageQueueAccessRights]::FullControl, [System.Messaging.AccessControlEntryType]::Allow)
}
elseif ($opeartion -ieq "-d")
{
    Write-Host "Delete Queue: " + $queuename
    [System.Messaging.MessageQueue]::Delete($queuename)
}
